//package com.plian.system.service.pm.assetleasingaudit.impl;
//
//
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.gson.GsonBuilder;
//import com.itextpdf.text.pdf.AcroFields;
//import com.itextpdf.text.pdf.BaseFont;
//import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.DateUtil;
//import com.plian.Tools.Func;
//import com.plian.Tools.StringPool;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.common.status.MyCustomCode;
//import com.plian.system.config.exception.MyRuntimeException;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dao.bt.baseinfo.BaseInfoDao;
//import com.plian.system.entity.pm.assetleasingaudit.AssetleasingAudit;
//import com.plian.system.mapper.pm.assetleasingaudit.AssetleasingAuditMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ListResult;
//import com.plian.system.model.ObjResult;
//import com.plian.system.model.page.Page;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.pdf.OutPdf;
//import com.plian.system.service.pm.assetleasingaudit.IAssetleasingAuditService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.vo.pm.assetleasingaudit.AssetleasingAuditVO;
//import com.plian.system.wrapper.pm.assetleasingaudit.AssetleasingAuditWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.apache.commons.io.output.ByteArrayOutputStream;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.springframework.core.io.DefaultResourceLoader;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.OutputStream;
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//import static com.plian.system.constant.sys.FormTypeConstant.CQZC_SSETLEASING_AUDIT;
//import static com.plian.system.pdf.OutPdf.processDate;
//
///**
// * 资产租赁公告审核主表 服务实现类
// *
// * @author gangan
// * @since 2019-10-12
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class AssetleasingAuditServiceImpl extends BaseServiceImpl<AssetleasingAuditMapper, AssetleasingAudit> implements IAssetleasingAuditService, BaseFlowService {
//
//    private  AssetleasingAuditMapper assetleasingAuditMapper;
//
//    private IFormCodeService formCodeService;
//
//    private BaseInfoDao baseInfoDao;
//
//    private AssetleasingAuditWrapper assetleasingAuditWrapper;
//    @Override
//    public boolean saveOrUpdate(AssetleasingAudit assetleasingAudit) {
//        if (!Optional.ofNullable(assetleasingAudit.getId()).isPresent()) {
//            assetleasingAudit.setVersion("1");
//            assetleasingAudit.setCode(formCodeService.getCode(FormTypeConstant.CQZC_SSETLEASING_AUDIT));
//            assetleasingAudit.setFormType(String.valueOf(FormTypeConstant.CQZC_SSETLEASING_AUDIT));
//        }
//
//        if (Optional.ofNullable(assetleasingAudit.getStatus()).isPresent() && assetleasingAudit.getStatus() != -1) {
//            assetleasingAudit.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//
//        return super.saveOrUpdate(assetleasingAudit);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            AssetleasingAudit assetleasingAudit = getById(id);
//            if (assetleasingAudit == null) {
//                return false;
//            } else if (assetleasingAudit.getStatus() != null &&
//                    (assetleasingAudit.getStatus() != FormStatusConstant.STASH && assetleasingAudit.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//        }
//        return super.deleteLogic(ids);
//    }
//
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean checkIsExisted(Long id, String code) {
//        if (Optional.ofNullable(code).isPresent()) {
//            AssetleasingAudit assetleasingAudit = new AssetleasingAudit();
//            assetleasingAudit.setCode(code);
//            if (Optional.ofNullable(getOne(Condition.getQueryWrapper(assetleasingAudit))).isPresent()) {
//                if (id == null || !id.equals(getOne(Condition.getQueryWrapper(assetleasingAudit)).getId())) {
//                    return false;
//                }
//            }
//        }
//        return true;
//    }
//
//
//    @Override
//    public ObjResult checkSubmit(AssetleasingAudit assetleasingAudit) {
//        if (!checkIsExisted(assetleasingAudit.getId(), assetleasingAudit.getCode())) {
//            return new ObjResult(MyCustomCode.BILL_ALREADY_EXISTS2);
//        }
//        return null;
//    }
//
//    @Override
//    public ObjResult getDetailObj(AssetleasingAuditVO assetleasingAuditVO) {
//        if (!Optional.ofNullable(assetleasingAuditVO).isPresent()) {
//            return new ObjResult(CommonCode.FAIL);
//        }
//        PageData pd = new PageData();
//        ObjectMapper mapper = new ObjectMapper();
//        String json = JSON.toJSONStringWithDateFormat(assetleasingAuditVO, DateUtil.PATTERN_DATETIME);
//        try {
//            pd.putAll(mapper.readValue(json, new TypeReference<HashMap<String, String>>() {
//            }));
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return new ObjResult(CommonCode.ERROR);
//        }
//        ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
//        objResult.setPageData(pd);
//        return objResult;
//    }
//
//
//    /**
//     * @param formIds
//     * @return
//     */
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        LambdaQueryWrapper<AssetleasingAudit> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(AssetleasingAudit::getId, Func.toLongList(org.apache.commons.lang3.StringUtils.join(formIds, StringPool.COMMA)));
//        List<AssetleasingAudit> assetleasingAudit = list(queryWrapper);
//        List<AssetleasingAuditVO> assetleasingAuditVOS = assetleasingAuditWrapper.entityToVO(assetleasingAudit);
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(assetleasingAuditVOS)) {
//                for (AssetleasingAuditVO vo : assetleasingAuditVOS) {
//                    vo.setUNITNAME(vo.getReportCompanyName());
//                    vo.setAPPLICANTNAME(vo.getCreateUserName());
//                    PageData pd = new PageData();
//                    ObjectMapper mapper = new ObjectMapper();
//                    String json = new GsonBuilder().setDateFormat(DateUtil.PATTERN_DATETIME).create().toJson(vo);
//                    pd.put("orgName",vo.getReportCompanyName());
//                    pd.putAll(mapper.readValue(json, new TypeReference<HashMap<String, String>>() {
//                    }));
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(CQZC_SSETLEASING_AUDIT);
//    }
//
//    @Override
//    public File buildpdf(Long id) {
//        File file = null ;
//        try {
//            AssetleasingAudit assetleasingAudit = getById(id);
//            if (assetleasingAudit!=null && !"".equals(assetleasingAudit)) {
//                AssetleasingAuditVO assetleasingAuditVO = assetleasingAuditWrapper.entityToVO(assetleasingAudit);
//                String transactionMeans=assetleasingAuditVO.getTransactionMeans();
//                assetleasingAuditVO.setTransactionMeans(baseInfoDao.findbycode(transactionMeans).getString("name"));
//
//                Map< String,Object > data = new HashMap< String,Object >();
//                data.putAll(OutPdf.beanToMap(assetleasingAuditVO));
//                ResourceLoader resourceLoader = new DefaultResourceLoader();
//                File model = File.createTempFile("logo", ".pdf");
//                try {
//                    FileUtils.copyInputStreamToFile(resourceLoader.getResource("/pdf/assetleasing_audit.pdf").getInputStream(), model);
//                } finally {
//                    //IOUtils.closeQuietly(inputStream);
//                }
//                String fileName = model.getPath();
//                PdfReader reader = new PdfReader(fileName);
//                ByteArrayOutputStream bos = new ByteArrayOutputStream();
//                /* 将要生成的目标PDF文件名称 */
//                PdfStamper ps = new PdfStamper(reader, bos);
//                /* 使用中文字体 */
//                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//                ArrayList < BaseFont > fontList = new ArrayList < BaseFont > ();
//                fontList.add(bf);
//
//                /* 取出报表模板中的所有字段 */
//                AcroFields fields = ps.getAcroFields();
//                fields.setSubstitutionFonts(fontList);
//                for (String key: data.keySet()) {
//                    String value = data.get(key).toString();
//                    if(data.get(key) instanceof Date){
//                        value=processDate((Date)data.get(key));
//                    }
//                    fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
//                }
//
//                ps.setFormFlattening(true);
//                ps.close();
//                File dir = new File(model.getParent());
//                file = File.createTempFile("assetleasing_audit", ".pdf", dir);
//                OutputStream fos = new FileOutputStream(file);
//                fos.write(bos.toByteArray());
//                fos.flush();
//                fos.close();
//                bos.close();
//            }
//
//        } catch(Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return file;
//    }
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    @Override
//    public ListResult<PageData> reportList(PageData pd) {
//        Page page=new Page(false,pd);
//        try {
//            List<PageData> list= assetleasingAuditMapper.reportListPage(page);
//            ListResult listResult=new ListResult(CommonCode.SUCCESS);
//            listResult.setList(list);
//            listResult.setTotal(page.getTotal());
//            return listResult;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            throw new MyRuntimeException();
//        }
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 判断状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            AssetleasingAudit assetleasingAudit = getById(id);
//            assetleasingAudit.setStatus(updateStatus);
//            updateById(assetleasingAudit);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//}
